﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
using OrderApp;

namespace OrderAppWin
{
    public partial class OrderItems : Form
    {

        public  Order CurrentOrder { get; set; }
        public OrderItems()
        {
            InitializeComponent();
            cusbindingSource.Add(new Cus("1", "Jimin"));
            cusbindingSource.Add(new Cus("2", "V"));
        }

        public OrderItems(Order order,bool alterMode = false) : this()
        {
            CurrentOrder = order;
            orderbindingSource.DataSource = CurrentOrder;
            id.Enabled = !alterMode;
            if (!alterMode)
            {
              //  CurrentOrder.Customer = (Cus)cusbindingSource.Current;
            }

        }

        private void OrderItems_Load(object sender, EventArgs e)
        {

        }

        private void ok_Click(object sender, EventArgs e)
        {
            orderbindingSource.ResetBindings(true);   
            this.Close();
        }

        private void add_item_Click(object sender, EventArgs e)
        {
            Items items = new Items(new OrderItem());
            try
            {
                if (items.ShowDialog() == DialogResult.OK)
                {
                    uint index = 0;
                    if (CurrentOrder.Items.Count != 0)
                    {
                        index = CurrentOrder.Items.Max(i => i.Index) + 1;

                    }
                    items.orderItem.Index = index;
                    CurrentOrder.AddItem(items.orderItem);
                    itembindingSource.ResetBindings(false);
                }
            }
            catch(Exception e2)
            {
                MessageBox.Show(e2.Message);
            }
        }
    }
}
